package com.qcxk.yuqian.system.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qcxk.yuqian.system.component.behaviour.BehaviourOperator;
import com.qcxk.yuqian.system.component.constant.SystemConstant;
import com.qcxk.yuqian.system.entity.SysSet;
import com.qcxk.yuqian.system.entity.User;
import com.qcxk.yuqian.system.service.SysSetService;
import com.qcxk.yuqian.system.utils.ThmsUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("/sysSet")
public class SysSetController {
    @Autowired
    private SysSetService sysSetService;
    @BehaviourOperator(desc = "新增参数")
    @RequestMapping(value = "/addSysSet")
    public JSONObject addSysSet(SysSet sysSet) {
        JSONObject json = new JSONObject();
        sysSet.setCtime(ThmsUtil.getDate());

        QueryWrapper<SysSet> userQueryWrapper = new QueryWrapper<>();
        userQueryWrapper.eq("name", sysSet.getName());

        SysSet ss = sysSetService.getOne(userQueryWrapper);
        if(ss==null){
            if (sysSetService.save(sysSet)) {
                json.put("code", "0");
            } else {
                json.put("code", "1");
                json.put("msg", SystemConstant.SYS_DATABASE_ERROR_CN);
            }
        }else {
            json.put("code", "1");
            json.put("msg", "参数已存在");
        }

        return json;
    }
    @BehaviourOperator(desc = "修改参数")
    @RequestMapping(value = "/updateSysSet")
    public JSONObject updateSysSet(SysSet sysSet) {
        JSONObject json = new JSONObject();
        sysSet.setUtime(ThmsUtil.getDate());
        if (sysSetService.updateById(sysSet)) {
            json.put("code", "0");
        } else {
            json.put("code", "1");
            json.put("msg", SystemConstant.SYS_DATABASE_ERROR_CN);
        }
        return json;
    }
    @BehaviourOperator(desc = "查询参数信息")
    @RequestMapping(value = "/loadSysSet")
    public JSONObject loadOrgan(SysSet sysSet) {
        JSONObject json = new JSONObject();
        sysSet = sysSetService.getById(sysSet.getId());
        if (sysSet != null) {
            json.put("code", "0");
            json.put("data", sysSet);
        } else {
            json.put("code", "1");
            json.put("msg", SystemConstant.SYS_DATABASE_ERROR_CN);
        }
        return json;
    }

    @BehaviourOperator(desc = "查询参数列表")
    @RequestMapping(value = "/querySysSetList")
    public JSONObject querySysSetList(Page page, SysSet sysSet) {
        JSONObject json = new JSONObject();
        page = sysSetService.querySysSetList(page, sysSet);
        json.put("code", "0");
        json.put("current", page.getCurrent());
        json.put("size", page.getSize());
        json.put("total", page.getTotal());
        json.put("dataList", page.getRecords());
        return json;
    }
}
